#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


#include "../include/phone.h"

/*显示全部信息*/
void show_item(PHONE *head)
{
    printf("name:\t%s\n", head->name);
    printf("phone-num:\t%s\n", head->phone_number);
    printf("home-addr:\t%s\n", head->home_address);
    printf("company-num:\t%s\n", head->company_number);
    
}

/**
 * 函数：初始化链表
 * 参数：
 *      -head：需要初始化的虚拟头指针
 */
int init(PHONE **head)
{
    PHONE *newnode = (PHONE *)malloc(sizeof(PHONE));
    if(NULL == newnode)
    {
        return -1;
    }
    newnode->next = NULL;
    /*将头指针初始化为虚拟头指针，即头指针不包含真实的用户信息*/
    *head = newnode;
    return 0;
}

/*
 * 函数：新建用户信息
 * 参数：
 *      -head：链表头指针
 * 说明：采用尾插法插入单向链表
 */
int create(PHONE * head)
{
    //PHONE *p;
    //p = head;
    PHONE *newnode = (PHONE *)malloc(sizeof(PHONE));
    if(NULL == newnode)
    {
        return -1;
    }

    
    printf("please input:\n");
    srand((unsigned)time(NULL));
    newnode->id = rand() % 100;
    printf("id = %d\n", newnode->id);

    printf("name:");
    scanf("%s", newnode->name);
    //fgets(newnode->name, 20, stdin);
    
    printf("phone_num:");
    scanf("%s", newnode->phone_number);
    //fgets(newnode->phone_number, 20, stdin);
    
    printf("home_addr:");
    scanf("%s", newnode->home_address);
    //fgets(newnode->home_address, 20, stdin);

    printf("company_num:");
    scanf("%s", newnode->company_number);
    //fgets(newnode->company_number, 20, stdin);

        
    newnode->next = NULL;
    while(head->next != NULL)
    {
        head = head->next;
    }
    head->next = newnode;

    printf("----created----");
    printf("please hit [enter] key twice and return to menu!\n");
    getchar();
    getchar();
    //head = p;
    return 0;
}

int delete(PHONE * head)
{
    //PHONE *p;
    //p = head;
    char name[20] = {0};
    int count = 0;
    PHONE *tmp = NULL;
    printf("please input one name that you want to delete:\n");
    scanf("%s", name);
    //fgets(name, 20, stdin);

    /*查找相同名字的链表节点
     *1.使用strcmp函数对比name，找到相同名字的节点，
     *  删除节点，并释放该节点的堆内存空间
     *2.未找到相同名字的节点，在循环外部判断count值是否为0
     *  输出提示字符*/
    while(head->next != NULL)
    {
        if(strcmp(head->next->name, name) == 0)
        {
            /*找到相同名字的节点*/
            tmp = head->next;
            count++;
            head->next = tmp->next;
            free(tmp);
            continue;
        }
        head = head->next;
    }
    if(count == 0)
    {
        printf("no this one name.\n");
    }
    else
    {
        printf("%d item is deleted!\n", count);
    }
    //head = p;
    printf("please hit [enter] key twice and return to menu!\n");
    getchar();
    getchar();
    //printf("\n");
    return 0;
    
}




/*匹配搜索名字，打印节点信息*/
int search(PHONE * head)
{
    //PHONE *p;
    //p = head;
    char name[20] = {0};
    int count = 0;
    PHONE *tmp = NULL;
    printf("please input one name that you want to search:\n");
    scanf("%s", name);

    /*查找相同名字的链表节点*/
    while(head->next != NULL)
    {
        if(strcmp(head->next->name, name) == 0)
        {
            /*找到相同名字的节点*/
            tmp = head->next;
            count++;
            show_item(tmp);
        }
        head = head->next;
    }
    if(count == 0)
    {
        printf("no this one name.\n");
    }
    else
    {
        printf("%d item[s] searched!\n", count);
    }
    //head = p;
    printf("please hit [enter] key twice and return to menu!\n");
    getchar();
    getchar();
    //printf("\n");
    return 0;
    
}

/*显示所有节点信息*/
int display(PHONE * head)
{
    //PHONE *p;
    //p = head;
    int num = 0;

    if(head == NULL)
    {
        printf("no poeple\n");
        return -1;
    }

    while(head->next != NULL)
    {
        printf("---------------------[%d]---------------------\n", num++);
        show_item(head->next);
        head = head->next;
    }
    printf("---------------------end---------------------\n");
    printf("please hit [enter] key twice and return to menu!\n");
    getchar();
    getchar();

    //head = p;
    
    return 0;
        
}


/*销毁所有节点信息*/
PHONE *allfree(PHONE *head)
{
    PHONE *tmp = NULL;
    while(head->next != NULL)
    {
        tmp = head;
        head =head->next;
        free(tmp);
    }
    free(head);
    head = NULL;
    return head;
}




